package com.lcn.controller;

import com.lcn.base.BaseInfoProperties;
import com.lcn.enums.YesOrNo;
import com.lcn.grace.result.GraceJSONResult;
import com.lcn.grace.result.ResponseStatusEnum;
import com.lcn.pojo.Friendship;
import com.lcn.pojo.vo.ContactsVO;
import com.lcn.service.FriendshipService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/friendship")
@Slf4j
public class FriendshipController extends BaseInfoProperties {

    @Resource
    private FriendshipService friendshipService;

    /**
     * 查询好友关系
     *
     * @param friendId
     * @param request
     * @return
     */
    @PostMapping("/getFriendship")
    public GraceJSONResult getFriendship(String friendId, HttpServletRequest request) {
        String myId = request.getHeader(HEADER_USER_ID);
        Friendship friendship = friendshipService.getFriendship(friendId, myId);
        return GraceJSONResult.ok(friendship);
    }

    /**
     * 查询通讯录
     *
     * @param request
     * @return
     */
    @PostMapping("/queryMyFriends")
    public GraceJSONResult queryMyFriends(HttpServletRequest request) {
        String userId = request.getHeader(HEADER_USER_ID);
        List<ContactsVO> contactsVOS = friendshipService.queryMyFriends(userId, false);
        return GraceJSONResult.ok(contactsVOS);
    }

    /**
     * 查询黑名单列表
     *
     * @param request
     * @return
     */
    @PostMapping("/queryMyBlackList")
    public GraceJSONResult queryMyBlackList(HttpServletRequest request) {
        String userId = request.getHeader(HEADER_USER_ID);
        List<ContactsVO> contactsVOS = friendshipService.queryMyFriends(userId, true);
        return GraceJSONResult.ok(contactsVOS);
    }

    /**
     * 修改备注
     *
     * @param friendId
     * @param friendRemark
     * @return
     */
    @PostMapping("/updateFriendRemark")
    public GraceJSONResult updateFriendRemark(String friendId, String friendRemark, HttpServletRequest request) {
        if (StringUtils.isBlank(friendId) || StringUtils.isBlank(friendRemark)) {
            return GraceJSONResult.error();
        }
        String myId = request.getHeader(HEADER_USER_ID);
        friendshipService.updateRemark(myId, friendId, friendRemark);
        return GraceJSONResult.ok();
    }

    /**
     * 拉入黑名单
     *
     * @param friendId
     * @param request
     * @return
     */
    @PostMapping("/tobeBlack")
    public GraceJSONResult tobeBlack(String friendId, HttpServletRequest request) {
       if (StringUtils.isBlank(friendId)) {
           return GraceJSONResult.error();
       }
       String myId = request.getHeader(HEADER_USER_ID);
       friendshipService.updateBlack(myId, friendId, YesOrNo.YES);
       return GraceJSONResult.ok();
    }

    /**
     * 移出黑名单
     *
     * @param friendId
     * @param request
     * @return
     */
    @PostMapping("/moveOutBlack")
    public GraceJSONResult moveOutBlack(String friendId, HttpServletRequest request) {
        if (StringUtils.isBlank(friendId)) {
            return GraceJSONResult.error();
        }
        String myId = request.getHeader(HEADER_USER_ID);
        friendshipService.updateBlack(myId, friendId, YesOrNo.NO);
        return GraceJSONResult.ok();
    }

    /**
     * 删除好友
     *
     * @param friendId
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public GraceJSONResult delete(String friendId, HttpServletRequest request) {
        if (StringUtils.isBlank(friendId)) {
            return GraceJSONResult.error();
        }
        String myId = request.getHeader(HEADER_USER_ID);
        friendshipService.delete(myId, friendId);
        return GraceJSONResult.ok();
    }

}
